package com.zuihai.app.mvp.ui.activity.find;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.zuihai.app.R;
import com.zuihai.app.di.component.DaggerFindRecyclerComponent;
import com.zuihai.app.di.module.FindRecyclerModule;
import com.zuihai.app.mvp.contract.FindRecyclerContract;
import com.zuihai.app.mvp.presenter.FindRecyclerPresenter;
import com.zuihai.app.mvp.ui.adapter.FindHomeRecyclerAdapter;
import com.zuihai.app.mvp.ui.adapter.FindRecyclerAdapter;
import com.zuihai.app.mvp.ui.utils.DividerListItemDecoration;
import com.zuihai.app.mvp.ui.widget.MyToast;
import com.zuihai.app.mvp.ui.widget.SimpleLoadMore;
import com.zuihai.app.mvp.ui.widget.SimpleRefresh;
import com.zuihai.app.mvp.ui.widget.behavior.listener.IsChildScrollListener;
import com.zuihai.app.mvp.ui.widget.behavior.listener.NeedExpandListener;
import com.zuihai.app.mvp.ui.widget.behavior.listener.SupportNeedExpendListener;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/5/4 0004.
 */

public abstract class FindRecyclerFragment extends BaseFragment<FindRecyclerPresenter> implements FindRecyclerContract.View, BaseQuickAdapter.OnItemClickListener, SupportNeedExpendListener, IsChildScrollListener {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    public static final int MIN_CLICK_DELAY_TIME = 1000;
    public long lastClickTime = 0;

    @BindView(R.id.refresh_layout)
    TwinklingRefreshLayout mRefreshLayout;

    NeedExpandListener mNeedExpandListener;

    private FindRecyclerAdapter mAdapter;

    //加载更多监听
    private BaseQuickAdapter.RequestLoadMoreListener mLoadMoreListener = new BaseQuickAdapter.RequestLoadMoreListener() {
        @Override
        public void onLoadMoreRequested() {
            requestData(false);
        }
    };

    public RecyclerView getRecycleView(){
        return  mRecyclerView;
    }
    public BaseQuickAdapter.RequestLoadMoreListener getmLoadMoreListener(){
        return mLoadMoreListener;
    }

    //刷新监听
    private RefreshListenerAdapter mRefresListener = new RefreshListenerAdapter() {
        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
            requestData(true);
        }
    };

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerFindRecyclerComponent
                .builder()
                .appComponent(appComponent)
                .findRecyclerModule(new FindRecyclerModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_find_recycler, container, false);
    }

    @Override
    protected void initData() {
        initRefreshLayout();
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.startRefresh();
            }
        });
    }

    /**
     * 初始化下拉刷新
     */
    private void initRefreshLayout() {
        mRefreshLayout.setHeaderView(new SimpleRefresh(getContext()));
        mRefreshLayout.setEnableOverScroll(false);
        mRefreshLayout.setEnableLoadmore(false);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                requestData(true);
            }
        });
    }

    @Override
    public void setNeedExpendListener(NeedExpandListener listener) {
        mNeedExpandListener = listener;
    }

    @Override
    public NeedExpandListener getNeedExpendListener() {
        return mNeedExpandListener;
    }

    @Override
    public boolean childScroll(boolean mUp, boolean refresnEnable) {
        mRefreshLayout.setEnableRefresh(refresnEnable);
        RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
        return adapter != null && adapter.getItemCount() > 0 && ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition() != 0;
    }

    /**
     * 根据不同类型的列表请求不同接口
     */
    protected abstract void requestData(boolean isRefresh);

    @Override
    public void bindAdapter(FindRecyclerAdapter adapter) {

        //添加条目点击事件
        adapter.setOnItemClickListener(this);
        //设置加载更多
        adapter.setOnLoadMoreListener(mLoadMoreListener, mRecyclerView);
        adapter.openLoadAnimation();
        adapter.setLoadMoreView(new SimpleLoadMore());
        mRecyclerView.setAdapter(adapter);
        //添加布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        //添加分割线
        DividerListItemDecoration decoration = new DividerListItemDecoration(getContext(), LinearLayoutManager.VERTICAL, R.drawable.common_divider_padding14_shape);
        mRecyclerView.addItemDecoration(decoration);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (mNeedExpandListener != null && dy < 0 && ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0) {
                    mNeedExpandListener.needExpand();
                }
            }
        });
    }

    @Override
    public void bindFHAdapter(FindHomeRecyclerAdapter adapter) {

    }

    @Override
    public void handleError(Throwable e) {
        mRefreshLayout.finishRefreshing();
    }

    @Override
    public void setData(Object data) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        //完成刷新
        mRefreshLayout.finishRefreshing();
    }

    @Override
    public void showMessage(String message) {
        if (!TextUtils.isEmpty(message)) {
            MyToast.showSuccess(message);
        }
    }
}